package com.niiwoo.civet.mobile.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.niiwoo.civet.account.dto.response.AccountBaseResponse;
import com.niiwoo.civet.account.dto.response.AccountCompanyDTO;
import com.niiwoo.civet.account.dto.response.GetSimpleUserBankCardResponse;
import com.niiwoo.civet.account.enums.AccountCompanyAccountTypeEnum;
import com.niiwoo.civet.account.service.AccountDubboService;
import com.niiwoo.civet.account.service.bankcard.BankCardDubboService;
import com.niiwoo.civet.mobile.vo.response.UserSecurityResponseVO;
import com.niiwoo.civet.trade.dto.common.InvestEvaluationDTO;
import com.niiwoo.civet.trade.service.invest.InvestDubboService;
import com.niiwoo.civet.account.service.AccountCompanyDubboService;
import com.niiwoo.tripod.base.component.PrivacyMasks;
import com.niiwoo.tripod.web.annotation.AuthIgnore;
import com.niiwoo.tripod.web.util.PrincipalContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.niiwoo.tripod.web.vo.Result;

@RestController
@RequestMapping("/user/security")
@Api(tags = "安全中心", description = "/user/security")
@Slf4j
public class UserSecurityController {

    //
    //@Autowired
    @Reference(version="1.0.0")
    private AccountCompanyDubboService accountCompanyDubboService;
    @Reference(version = "1.0.0")
    private AccountDubboService accountDubboService;
    @Reference(version = "1.0.0")
    private BankCardDubboService bankCardDubboService;
    @Reference(version = "1.0.0")
    private InvestDubboService investDubboService;
    @Autowired
    private PrivacyMasks privacyMasks;

    @PostMapping("/getUserStatus")
    @ApiOperation("获取用户安全信息")
    public Result<UserSecurityResponseVO> getUserStatus(){
        UserSecurityResponseVO result = new UserSecurityResponseVO();
        String userId = PrincipalContext.getUserId();
        UserSecurityResponseVO.BankAccount bankAccount = new UserSecurityResponseVO().new BankAccount();
        UserSecurityResponseVO.BankCard bankCard = new UserSecurityResponseVO().new BankCard();
        UserSecurityResponseVO.BankLinkman bankLinkman = new UserSecurityResponseVO().new BankLinkman();
        UserSecurityResponseVO.RiskAssessment riskAssessment = new UserSecurityResponseVO().new RiskAssessment();

        AccountCompanyDTO accountCompanyDTO = accountCompanyDubboService.queryAccountCompanyByUserIdAndOrgIdWithAccountType(userId,userId, AccountCompanyAccountTypeEnum.INVEST_ORG.getType());
        if(accountCompanyDTO == null ){
            result.setOpenAccount(false);
        }else{
            result.setOpenAccount(true);
            bankAccount.setStatus(accountCompanyDTO.getAuditStatus());
            bankAccount.setCompanyName(accountCompanyDTO.getCompanyName());
            if(null != accountCompanyDTO.getUnifiedCode() && !"".equals(accountCompanyDTO.getUnifiedCode())){
                bankAccount.setCreditCode(accountCompanyDTO.getUnifiedCode());
            }else{
                bankAccount.setCreditCode(accountCompanyDTO.getBusinessLicense());
            }

            AccountBaseResponse accountBaseResponse = accountDubboService.getOrgAccount(userId,userId);
            if (null != accountBaseResponse && accountBaseResponse.getTender().equals((byte) 1)) {
                result.setDirectInvest(true);
            } else {
                result.setDirectInvest(false);
            }
            try{
                GetSimpleUserBankCardResponse getSimpleUserBankCardResponse = bankCardDubboService.getSimpleUserBankCard(userId,userId);
                if( null != getSimpleUserBankCardResponse ){

                    bankCard.setBankName(getSimpleUserBankCardResponse.getBankCard().getBankType().getBankName());
                    bankCard.setBankCardTailNumber(getSimpleUserBankCardResponse.getBankCard().getBankAccountNo());

                }
            }catch (Exception e){
                log.error("异常不需要处理，查询绑卡时，企业用户没有绑卡load抛出异常 userId-orgId={}",userId);
            }
            //银行预留联通系人
            bankLinkman.setLinkmanName(accountCompanyDTO.getContactName());
            bankLinkman.setLinkmanMobile(privacyMasks.decryptPrivacy(accountCompanyDTO.getContactMobileNo()));
        }
        // 风险评测
        InvestEvaluationDTO investEvaluation = investDubboService.getInvestEvaluationInfo(userId);
        if( null != investEvaluation && investEvaluation.getHadEvaluate() != 0 ){
            riskAssessment.setRiskAssessmentResult(investEvaluation.getLevel());
            riskAssessment.setResidueDegree( investEvaluation.getCurrentYearTimes());
        }
        result.setBankAccount(bankAccount);
        result.setBankCard(bankCard);
        result.setBankLinkman(bankLinkman);
        result.setRiskAssessment(riskAssessment);
        result.setCompanyInfo("");
        result.setIsSetLoginPassword("已设置");
        return Result.with(result);
    }
}
